package com.walnut.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.support.AopUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

/**
 * Helper tools
 * Created by parker on 15/4/12.
 */
public class Utils {
    /**
     * set the member variables of the speiciic object default values
     * @param o         the object whose memeber variables will be changed
     * @param clazz     the class type of the object
     * @param fields    the name of member variable whose value will NOT be changed
     */
    public static void setClassFieldsValueDefault(Object o, Class clazz, String fields){
        if(null != fields && fields.length() > 0){
            String[] fieldArray = fields.split(",");
            if(null != fieldArray && fieldArray.length > 0) {
                List<String> fieldList = new ArrayList<>(Arrays.asList(fieldArray));
                Map<String, Method> tobeChangedMap = new HashMap<>(0);

                Method[] publicMethods = clazz.getMethods();

                String methodName, fieldName;
                Method method;
                for(int i=0;i<publicMethods.length;i++){
                    method = publicMethods[i];
                    methodName = method.getName();
                    System.out.println("methodName: " + methodName);
                    if(methodName.startsWith("set")){
                        fieldName = methodName.substring(3);
                        if(!fieldList.contains(StringUtils.uncapitalize(fieldName))){
                            System.out.println("fieldName: " + fieldName);
                            System.out.println("return type: " + method.getGenericReturnType().getTypeName());
                            try {
                                if(Integer.class.getName().equals(method.getGenericReturnType().getTypeName())
                                        || Long.class.getName().equals(method.getGenericReturnType().getTypeName())
                                        || Byte.class.getName().equals(method.getGenericReturnType().getTypeName()))
                                {
                                    method.invoke(o, 0);
                                }
                                else if(Float.class.getName().equals(method.getGenericReturnType().getTypeName())
                                        || Double.class.getName().equals(method.getGenericReturnType().getTypeName())){
                                    method.invoke(o, 0.0);
                                }
                                else {
                                    method.invoke(o, new Object[]{});
                                }
                            }
                            catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                            catch (InvocationTargetException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }
    }
}
